package com.bt7274.controller;

import com.bt7274.grace.result.GraceJSONResult;
import com.bt7274.pojo.TorrentComment;
import com.bt7274.pojo.bo.TorrentCommentBO;
import com.bt7274.pojo.vo.TorrentCommentVO;
import com.bt7274.service.TorrentCommentService;
import com.bt7274.service.TorrentService;
import com.bt7274.service.UserTorrentFavoriteService;
import com.bt7274.service.UserTorrentLikeService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Data 2025/6/10 12:04
 * @Author hp
 * @Version 1.0
 * @Description TorrentInteractionController
 */

@RestController
@RequestMapping("torrent")
public class TorrentInteractionController {
    @Resource
    private UserTorrentLikeService userTorrentLikeService;
    @Resource
    private UserTorrentFavoriteService userTorrentFavoriteService;
    @Resource
    private TorrentCommentService torrentCommentService;

    // 用户点赞种子
    @PostMapping("/{torrentId}/like")
    public GraceJSONResult likeTorrent(@RequestParam String userId, @PathVariable Long torrentId) {
        userTorrentLikeService.likeTorrent(userId, torrentId);
        return GraceJSONResult.ok("点赞成功");
    }

    // 用户取消点赞种子
    @PostMapping("/{torrentId}/unlike")
    public GraceJSONResult unlikeTorrent(@RequestParam String userId, @PathVariable Long torrentId) {
        userTorrentLikeService.unlikeTorrent(userId, torrentId);
        return GraceJSONResult.ok("取消点赞成功");
    }

    // 获取种子点赞数
    @GetMapping("/{torrentId}/likeCount")
    public GraceJSONResult getTorrentLikeCount(@PathVariable Long torrentId) {
        Long likeCount = userTorrentLikeService.getTorrentLikeCount(torrentId);
        return GraceJSONResult.ok(likeCount);
    }

    // 用户收藏种子
    @PostMapping("/{torrentId}/favorite")
    public GraceJSONResult favoriteTorrent(@RequestParam String userId, @PathVariable Long torrentId) {
        userTorrentFavoriteService.favoriteTorrent(userId, torrentId);
        return GraceJSONResult.ok("收藏成功");
    }

    // 用户取消收藏种子
    @PostMapping("/{torrentId}/unfavorite")
    public GraceJSONResult unfavoriteTorrent(@RequestParam String userId, @PathVariable Long torrentId) {
        userTorrentFavoriteService.unfavoriteTorrent(userId, torrentId);
        return GraceJSONResult.ok("取消收藏成功");
    }

    // 获取种子收藏数
    @GetMapping("/{torrentId}/favoriteCount")
    public GraceJSONResult getTorrentFavoriteCount(@PathVariable Long torrentId) {
        Long favoriteCount = userTorrentFavoriteService.getTorrentFavoriteCount(torrentId);
        return GraceJSONResult.ok(favoriteCount);
    }

    // 用户添加评论
    @PostMapping("/{torrentId}/comment/add")
    public GraceJSONResult addTorrentComment(@RequestBody TorrentCommentBO commentBO) {
        TorrentCommentVO commentVO  = torrentCommentService.addComment(commentBO);
        return GraceJSONResult.ok(commentVO);
    }

    // 用户删除评论
    @PostMapping("/{torrentId}/comment/delete")
    public GraceJSONResult deleteTorrentComment(@RequestParam Long commentId) {
        // 检查评论是否存在
        boolean commentExists = torrentCommentService.commentExists(commentId);
        if (!commentExists) {
            return GraceJSONResult.errorMsg("评论不存在");
        }
        torrentCommentService.deleteCommentById(commentId);
        return GraceJSONResult.ok("删除成功");
    }

    // 获取种子评论列表
    @GetMapping("/{torrentId}/comment/list")
    public GraceJSONResult getTorrentComments(@PathVariable Long torrentId) {
        List<TorrentCommentVO> comments = torrentCommentService.getCommentsByTorrentId(torrentId);
        return GraceJSONResult.ok(comments);
    }


}